package com.gruppsex.trainingdiary;

import java.util.Calendar;

import com.gruppsex.trainingdairy.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.NumberPicker;

public class TestActivity extends Activity {
	private ListView listView;
	private Calendar calendar;
	private ArrayAdapter<String> adapter;
	private DatePicker datePicker;
	private String[] activitys = { "Monday", "Thuesday", "Wednesday",
			"Thursday", "Friday", "Saturday", "Sunday" };
	private NumberPicker np;
	private String[] nums = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
			"10", "11", "12" };
	private int day;
	private String weekDay;
	private String date = "22-3-2013";
	private int month;
	private int year;
	private Intent myIntent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		overridePendingTransition(R.anim.fadein, R.anim.fadeout);
		setContentView(R.layout.test);
		np = (NumberPicker) findViewById(R.id.numberPicker1);
		datePicker = (DatePicker) findViewById(R.id.datePicker1);
		np.setMinValue(0);
		np.setMaxValue(12);
		np.setWrapSelectorWheel(false);
		np.setDisplayedValues(nums);
		np.setValue(1);
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, activitys);
		listView = (ListView) findViewById(R.id.listViewWeeks);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				weekDay = listView.getItemAtPosition(position).toString();
				
				Intent intent = new Intent(getApplicationContext(),
						WorkOutActivities.class);

				intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
				startActivity(intent);
			}
		});
	}

	public String getDateFromDatePicket(DatePicker datePicker) {
		day = datePicker.getDayOfMonth();
		month = datePicker.getMonth();
		year = datePicker.getYear();
		calendar = Calendar.getInstance();
		calendar.set(year, month, day);
		day = calendar.get(calendar.DATE);
		month = calendar.get(calendar.MONTH);
		year = calendar.get(calendar.YEAR);
		String s = "" + day + "" + (month + 1) + "" + year;
		return s;
	}

	public String getNewDate(int days) {
		int mDay = 0;
		int mMonth = 0;
		int mYear = 0;
		calendar = Calendar.getInstance();
		calendar.set(year, month, day);
		calendar.add(calendar.DATE, days);
		mDay = calendar.get(calendar.DATE);
		mMonth = calendar.get(calendar.MONTH);
		mYear = calendar.get(calendar.YEAR);
		String s = "" + mDay + "-" + (mMonth + 1) + "-" + mYear;
		return s;
	}

	public String getValueFromNP(int weeks) {
		String s = "";
		if (weeks == 0) {
			s = getNewDate(0);
		} else if (weeks == 1) {
			s = getNewDate(7);
		} else if (weeks == 2) {
			s = getNewDate(7 * 2);
		} else if (weeks == 2) {
			s = getNewDate(7 * 2);
		} else if (weeks == 3) {
			s = getNewDate(7 * 3);
		} else if (weeks == 4) {
			s = getNewDate(7 * 4);
		} else if (weeks == 5) {
			s = getNewDate(7 * 5);
		} else if (weeks == 6) {
			s = getNewDate(7 * 6);
		} else if (weeks == 7) {
			s = getNewDate(7 * 7);
		} else if (weeks == 8) {
			s = getNewDate(7 * 8);
		} else if (weeks == 9) {
			s = getNewDate(7 * 9);
		} else if (weeks == 10) {
			s = getNewDate(7 * 10);
		} else if (weeks == 11) {
			s = getNewDate(7 * 11);
		} else if (weeks == 12) {
			s = getNewDate(7 * 12);
		}
		return s;
	}

	public void onBtnClicked(View v) {

		switch (v.getId()) {
		case R.id.btn_saveWorkout:
			String[] activitys;
			day = datePicker.getDayOfMonth();
			month = datePicker.getMonth();
			year = datePicker.getYear();
			int weeks = 0;
			try {
				Bundle extras = getIntent().getExtras();
				activitys = extras.getStringArray("WorkOutActivity.activitys");
				weeks = np.getValue();
				DataBaseHelper dbHelper = new DataBaseHelper(this);
				dbHelper.openDataBase();
				for (int i = 0; i < activitys.length; i++) {
					for (int j = 0; j <= weeks; j++) {
						date = getValueFromNP(j);
						dbHelper.createDayPassEntry(weekDay, activitys[i],
								date, 0, 0, "");
					}
				}
				dbHelper.close();
				break;
			} catch (Exception e) {
			} finally {
				myIntent = new Intent(v.getContext(), StartActivity.class);
				v.getContext().startActivity(myIntent);
			}
		}
	}
}
